package com.spark.lola.controller.logistics;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spark.lola.logistics.entity.CarSubscribe;
import com.spark.lola.logistics.service.CarSubscribeService;
import com.spark.lola.support.common.constant.CommonConstant;
import com.spark.lola.support.common.entity.ShiroUser;
import com.spark.lola.support.common.util.ResponseBean;
import com.spark.lola.support.mybatis.entity.QueryParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @author: Zhi-peng wang
 * @date: 2020/05/12 11:26
 * 回头车预订接口
 **/
@RestController
@RequestMapping("/api/logistics/car-subscribe")
@Api(value = "/api/logistics/car-subscribe", tags = "回头车预订接口")
public class CarSubscribeController {

    @Autowired
    private CarSubscribeService carSubscribeService;

    @ApiOperation(value = "分页查询回头车预订信息")
    @RequiresPermissions(value = "m5170")
    @RequestMapping(value = "/findBy", method = RequestMethod.POST)
    public ResponseBean findPage(@RequestBody QueryParam<CarSubscribe> queryParam) {
        IPage<CarSubscribe> carSubscribeIPage = carSubscribeService.findPage(queryParam);
        return ResponseBean.success(carSubscribeIPage);
    }

    @ApiOperation(value = "预订审核")
    @RequiresPermissions(value = "m5172")
    @RequestMapping(value = "/audio", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean audio(@RequestBody JSONObject jsonObject, @SessionAttribute ShiroUser shiroUser) throws Exception {
        Long id = jsonObject.getLong("id");
        Boolean isPass = jsonObject.getBoolean("isPass");
        String msg = jsonObject.getString("msg");
        if (id == null || isPass == null) {
            return ResponseBean.error("参数错误");
        }
        if (!isPass && StringUtils.isBlank(msg)) {
            return ResponseBean.error("参数错误");
        }
        carSubscribeService.audio(id, msg, isPass, shiroUser);
        return ResponseBean.success();
    }

    @ApiOperation(value = "获取回头车预订信息")
    @ApiImplicitParam(name = "id", value = "回头车id", required = true, dataType = "long", paramType = "path")
    @RequiresPermissions(value = "m5171")
    @RequestMapping(value = "/findInfo/{id}", method = RequestMethod.GET)
    public ResponseBean findInfo(@PathVariable long id, @SessionAttribute ShiroUser shiroUser) {
        CarSubscribe carSubscribe = new CarSubscribe();
        if (id != 0) {
            carSubscribe = carSubscribeService.getById(id);
        }
        return ResponseBean.success(carSubscribe);
    }

}
